﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Hotel_后端.Controllers
{
    /// <summary>
    /// 图片上传
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UpImgController : ControllerBase
    {
        public UpImgController(IWebHostEnvironment webHostEnvironment)
        {
            WebHostEnvironment = webHostEnvironment;
        }

        public IWebHostEnvironment WebHostEnvironment { get; }

        [HttpPost]
        public IActionResult UpImg(IFormFile files)
        {
            var file = HttpContext.Request.Form.Files[0];
            var dir = WebHostEnvironment.WebRootPath + "/Imgs/";
            var filename = Guid.NewGuid() + file.FileName;
            var path = Path.Combine(dir, filename);
            using (var aa = new FileStream(path, FileMode.CreateNew)) ;
            return Ok(new { str = "上传成功", datas = "/Imgs/" + filename });
        }


        //[HttpPost]
        //public IActionResult UpImg(IFormFile file)
        //{
        //    //1.上传的是不是图片
        //    //获取图片格式
        //    var ImgType = file.GetType();
        //    //
        //    //
        //    //2.图片大小不可超过2M
        //    var m = 1024 * 1024 * 2; //2M
        //    if (file.Length > m)
        //    {
        //        return Ok(new {code =-1,msg = "上传图片不可大于2M" });
        //    }
        //    //3.完成图片上传

        //    //获取全球唯一图片
        //    var str = Guid.NewGuid();
        //    var path = Path.Combine("Imgs",str + file.FileName );
        //    using (var stream = new FileStream(path,FileMode.CreateNew))
        //    {
        //        file.CopyTo(stream);
        //    }
        //    return Ok(new { aaa = "上传成功", datas = "/Imgs" + str + file.FileName });
        //}



    }
}
